﻿using static WebApi2.WebApi.StaffInfoSimulation;

namespace WebApi2.WebApi
{
    public static class StaffInfoSimulation
    {
        public static RouteGroupBuilder MapStaffInfoApiV1(this IEndpointRouteBuilder app)
        {
            var api = app.MapGroup("dam-dataservice/open-api/dam/dataservice/defined/query-data");
            api.MapPost("EKJ3NRkdeRbzshiYEm4KAa6kRySGMhbH/v1", HandleStaffInfo);
            return api;
        }
        static Results<UnauthorizedHttpResult, Ok<ResponseModel<ResponseData>>> HandleStaffInfo(HttpRequest request, QueryInputModel query)
        {
            var authStr = request.Headers.Authorization.FirstOrDefault() ?? string.Empty;
            if (authStr != "Basic YWRtaW46MTIzNDU2")
            {
                return TypedResults.Unauthorized();
            }

            DataItem[] items = [
                new DataItem
                {
                    OdsMkjyZsjUserinfo = new OdsMkjyZsjUserinfo
                    {
                        BusinessData = new Businessdata
                        {
                            Birthday = "1990-01-01",
                            Phone = "010-12345678",
                            MobilePhone = "13800138000",
                            IdCard = "110101199001011234",
                            DeptName = "技术部",
                            AgentNo = "100001",
                            DeptId = "D001",
                            DisplayName = "张三",
                            JobTitle = "软件工程师"
                        }
                    }
                },
                new DataItem
                {
                    OdsMkjyZsjUserinfo = new OdsMkjyZsjUserinfo
                    {
                        BusinessData = new Businessdata
                        {
                            Birthday = "1985-05-20",
                            Phone = "010-87654321",
                            MobilePhone = "13900139000",
                            IdCard = "110101198505201234",
                            DeptName = "市场部",
                            AgentNo = "100002",
                            DeptId = "D002",
                            DisplayName = "李四",
                            JobTitle = "市场经理"
                        }
                    }
                }
            ];
            return TypedResults.Ok(new ResponseModel<ResponseData>
            {
                Data = new ResponseData
                {
                    Items = items.Where(m => m.OdsMkjyZsjUserinfo.BusinessData.IdCard == query.Filter?.IdCard
                    || m.OdsMkjyZsjUserinfo.BusinessData.MobilePhone == query.Filter?.MobilePhone
                    || m.OdsMkjyZsjUserinfo.BusinessData.AgentNo == query.Filter?.AgentNo).ToArray(),
                    Page = query.Page ?? 1,
                    PageSize = query.PageSize ?? 15,
                    Total = 2,
                    Count = 2
                }
            });
        }


        public class QueryInputModel
        {
            public int? Page { get; set; } = 1;
            public int? PageSize { get; set; } = 15;
            public string ModelCode { get; set; } = "ods_mkjy_zsj_userinfo";
            public DataFilter? Filter { get; set; }
            public object[]? queryField { get; set; } = [];
            public bool? Vague { get; set; } = false;
        }
        public class DataFilter
        {
            [JsonPropertyName("mobile_phone")]
            public string MobilePhone { get; set; } = "";
            [JsonPropertyName("id_card")]
            public string IdCard { get; set; } = "";
            [JsonPropertyName("agent_no")]
            public string AgentNo { get; set; } = "";
        }
        public class ResponseModel<T>
        {
            public int Status { get; set; } = 1;
            public string Message { get; set; } = "成功";
            public bool? AlreadyCompress { get; set; } = false;
            public T? Data { get; set; }
        }

        public class ResponseData
        {
            public int Page { get; set; } = 1;
            public int PageSize { get; set; } = 15;
            public int Total { get; set; } = 1180;
            public int Count { get; set; } = 15;
            public DataItem[] Items { get; set; } = [];
        }

        public class DataItem
        {
            [JsonPropertyName("ods_mkjy_zsj_userinfo")]
            public OdsMkjyZsjUserinfo OdsMkjyZsjUserinfo { get; set; } = new();
        }

        public class OdsMkjyZsjUserinfo
        {
            public Businessdata BusinessData { get; set; } = new Businessdata();
        }

        public class Businessdata
        {
            public string Birthday { get; set; } = "";
            public string Phone { get; set; } = "";
            [JsonPropertyName("mobile_phone")]
            public string MobilePhone { get; set; } = "";
            [JsonPropertyName("id_card")]
            public string IdCard { get; set; } = "";
            [JsonPropertyName("dept_name")]
            public string DeptName { get; set; } = "";
            [JsonPropertyName("agent_no")]
            public string AgentNo { get; set; } = "";
            [JsonPropertyName("dept_id")]
            public string DeptId { get; set; } = "";
            [JsonPropertyName("display_name")]
            public string DisplayName { get; set; } = "";
            [JsonPropertyName("job_title")]
            public string JobTitle { get; set; } = "";
        }

    }
}
